import { ERROR_CODE } from './constant';
import { showDialog, linkToHome } from '../utils/utils';

export async function request(name, params) {
    try {
        const res = await wx.cloud.callFunction({
            name,
            data: params
        });
        const { code, msg, data } = res.result;
        if (code === 1) {
            console.log(res.result)
            console.error(msg);
        } else {
            switch (code) {
                case ERROR_CODE.NOT_FOUND:
                    showDialog({
                        title: '当前画板不存在',
                        content: '请检查链接是否正确～',
                        successCb: linkToHome
                    });
                    break
                case ERROR_CODE.NOT_MEMBER:
                    showDialog({
                        title: '不是当前画板的成员',
                        content: `快去找${data.nickName}要权限吧～`,
                        successCb: linkToHome
                    });
                    break
                case ERROR_CODE.NOT_EDIT_AUTH:
                    showDialog({
                        title: '没有编辑权限',
                        content: `快去找${data.nickName}要权限吧～`,
                    });
                    break
                case ERROR_CODE.PAREM_EMPTY:
                    showDialog({
                        title: '参数为空',
                        content: '请输入正确参数',
                    });
                    break
            }
        }
        return data;
    } catch (err) {
        console.error(err);
    }
}
